<template>
    <div class="settings-page">
      <div class="settings-container">
        <div class="settings-header">
          <h2>个人设置</h2>
          <p>管理您的账户信息和偏好设置</p>
        </div>
        
        <div class="settings-content">
          <!-- 左侧栏：基本信息和账户信息 -->
          <div class="settings-left">
            <!-- 基本信息卡片 -->
            <div class="settings-card">
              <div class="card-header">
                <span>基本信息</span>
              </div>
              
              <div class="user-info-section">
                <div class="user-avatar">
                  <el-avatar :size="80" class="avatar">
                    <el-icon><User /></el-icon>
                  </el-avatar>
                  <div class="user-details">
                    <h3>{{ userStore.user?.username }}</h3>
                    <p>{{ userStore.user?.email }}</p>
                    <el-tag :type="userStore.user?.role_name === 'admin' ? 'danger' : 'info'" size="small">
                      {{ userStore.user?.role_name === 'admin' ? '管理员' : '普通用户' }}
                    </el-tag>
                  </div>
                </div>
                
                <div class="divider"></div>
                
                <el-form
                  ref="profileFormRef"
                  :model="profileForm"
                  :rules="profileRules"
                  label-width="auto"
                  class="profile-form"
                >
                  <el-form-item label="用户名">
                    <el-input v-model="profileForm.username" disabled class="disabled-input">
                      <template #suffix>
                        <span class="input-tip">不可修改</span>
                      </template>
                    </el-input>
                  </el-form-item>
                  
                  <el-form-item label="邮箱" prop="email">
                    <el-input v-model="profileForm.email" type="email" class="modern-input" />
                  </el-form-item>
                  
                  <el-form-item>
                    <el-button type="primary" :loading="profileLoading" @click="updateProfile" class="update-btn">
                      {{ profileLoading ? '更新中...' : '更新信息' }}
                    </el-button>
                  </el-form-item>
                </el-form>
              </div>
            </div>
            
            <!-- 账户信息卡片 -->
            <div class="settings-card">
              <div class="card-header">
                <span>账户信息</span>
              </div>
              
              <div class="account-info">
                <div class="info-item">
                  <div class="info-icon">
                    <el-icon><Calendar /></el-icon>
                  </div>
                  <div class="info-content">
                    <div class="info-label">注册时间</div>
                    <div class="info-value">{{ formatDate(userStore.user?.created_time) }}</div>
                  </div>
                </div>
                
                <div class="info-item">
                  <div class="info-icon">
                    <el-icon><Clock /></el-icon>
                  </div>
                  <div class="info-content">
                    <div class="info-label">最后登录</div>
                    <div class="info-value">{{ formatDate(userStore.user?.last_login_time) || '暂无记录' }}</div>
                  </div>
                </div>
                
                <div class="info-item">
                  <div class="info-icon">
                    <el-icon><CircleCheck /></el-icon>
                  </div>
                  <div class="info-content">
                    <div class="info-label">账户状态</div>
                    <el-tag :type="userStore.user?.is_active ? 'success' : 'danger'" size="small">
                      {{ userStore.user?.is_active ? '正常' : '已禁用' }}
                    </el-tag>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 右侧栏：修改密码 -->
          <div class="settings-right">
            <div class="settings-card password-card">
              <div class="card-header">
                <span>修改密码</span>
              </div>
              
              <el-form
                ref="passwordFormRef"
                :model="passwordForm"
                :rules="passwordRules"
                label-width="auto"
                class="password-form"
              >
                <el-form-item label="当前密码" prop="old_password">
                  <el-input
                    v-model="passwordForm.old_password"
                    type="password"
                    show-password
                    placeholder="请输入当前密码"
                    class="modern-input"
                  />
                </el-form-item>
                
                <el-form-item label="新密码" prop="new_password">
                  <el-input
                    v-model="passwordForm.new_password"
                    type="password"
                    show-password
                    placeholder="请输入新密码（至少6个字符）"
                    class="modern-input"
                  />
                </el-form-item>
                
                <el-form-item label="确认密码" prop="confirm_password">
                  <el-input
                    v-model="passwordForm.confirm_password"
                    type="password"
                    show-password
                    placeholder="请再次输入新密码"
                    class="modern-input"
                  />
                </el-form-item>
                
                <div class="form-actions">
                  <el-button type="primary" :loading="passwordLoading" @click="changePassword" class="password-btn">
                    {{ passwordLoading ? '修改中...' : '修改密码' }}
                  </el-button>
                  <el-button @click="resetPasswordForm" class="reset-btn">重置</el-button>
                </div>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import { ref, reactive, onMounted } from 'vue'
  import { ElMessage } from 'element-plus'
  import { User, Calendar, Clock, CircleCheck } from '@element-plus/icons-vue'
  import { useUserStore } from '@/stores/user'
  import { authService } from '@/services/auth'
  
  export default {
    name: 'UserSettings',
    components: {
      User,
      Calendar,
      Clock,
      CircleCheck
    },
    setup() {
      const userStore = useUserStore()
      const profileFormRef = ref()
      const passwordFormRef = ref()
      const profileLoading = ref(false)
      const passwordLoading = ref(false)
      
      // 个人信息表单
      const profileForm = reactive({
        username: '',
        email: ''
      })
      
      // 密码修改表单
      const passwordForm = reactive({
        old_password: '',
        new_password: '',
        confirm_password: ''
      })
      
      // 验证规则
      const profileRules = {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入有效的邮箱地址', trigger: 'blur' }
        ]
      }
      
      const validateConfirmPassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请确认新密码'))
        } else if (value !== passwordForm.new_password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }
      
      const passwordRules = {
        old_password: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        new_password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, message: '新密码至少需要6个字符', trigger: 'blur' }
        ],
        confirm_password: [
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
      
      // 初始化数据
      const initData = () => {
        if (userStore.user) {
          profileForm.username = userStore.user.username
          profileForm.email = userStore.user.email
        }
      }
      
      // 更新个人信息
      const updateProfile = async () => {
        if (!profileFormRef.value) return
        
        try {
          await profileFormRef.value.validate()
          profileLoading.value = true
          
          const updateData = {
            email: profileForm.email
          }
          
          const response = await authService.updateCurrentUser(updateData)
          userStore.updateUser(response)
          
          ElMessage.success('个人信息更新成功')
          
        } catch (error) {
          if (error.response && error.response.data) {
            ElMessage.error(error.response.data.detail || '更新失败')
          } else {
            ElMessage.error('更新失败，请重试')
          }
        } finally {
          profileLoading.value = false
        }
      }
      
      // 修改密码
      const changePassword = async () => {
        if (!passwordFormRef.value) return
        
        try {
          await passwordFormRef.value.validate()
          passwordLoading.value = true
          
          const passwordData = {
            old_password: passwordForm.old_password,
            new_password: passwordForm.new_password
          }
          
          await authService.changePassword(passwordData)
          
          ElMessage.success('密码修改成功')
          resetPasswordForm()
          
        } catch (error) {
          if (error.response && error.response.data) {
            ElMessage.error(error.response.data.detail || '密码修改失败')
          } else {
            ElMessage.error('密码修改失败，请重试')
          }
        } finally {
          passwordLoading.value = false
        }
      }
      
      // 重置密码表单
      const resetPasswordForm = () => {
        passwordForm.old_password = ''
        passwordForm.new_password = ''
        passwordForm.confirm_password = ''
        if (passwordFormRef.value) {
          passwordFormRef.value.clearValidate()
        }
      }
      
      // 格式化日期
      const formatDate = (dateString) => {
        if (!dateString) return ''
        return new Date(dateString).toLocaleString('zh-CN')
      }
      
      onMounted(() => {
        initData()
      })
      
      return {
        userStore,
        profileFormRef,
        passwordFormRef,
        profileLoading,
        passwordLoading,
        profileForm,
        passwordForm,
        profileRules,
        passwordRules,
        updateProfile,
        changePassword,
        resetPasswordForm,
        formatDate
      }
    }
  }
  </script>
  
  <style scoped>
.settings-page {
  height: calc(100vh - var(--header-height));
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 10px;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 移除背景动画以避免性能问题 */

.settings-container {
  max-width: 1000px;
  width: 100%;
  margin: 0 auto;
  position: relative;
  z-index: 1;
  padding: 0 8px;
  box-sizing: border-box;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.settings-header {
  text-align: center;
  margin-bottom: 8px;
  animation: slideDown 0.6s ease-out;
  padding: 4px 0;
}

.settings-header h2 {
  color: white;
  margin-bottom: 2px;
  font-weight: 600;
  font-size: 20px;
  letter-spacing: -0.5px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.settings-header p {
  color: rgba(255,255,255,0.8);
  font-size: 14px;
  line-height: 1.2;
  margin: 0;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.settings-header h2 {
  color: white;
  margin-bottom: 6px;
  font-weight: 700;
  font-size: 24px;
  letter-spacing: -0.5px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.settings-header p {
  color: rgba(255,255,255,0.8);
  font-size: 16px;
  line-height: 1.5;
}

.settings-content {
  display: flex;
  flex-direction: row;
  gap: 12px;
  animation: fadeIn 0.8s ease-out;
  align-items: stretch;
  max-height: calc(100vh - var(--header-height) - 80px);
  overflow: hidden;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.settings-left {
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 2;
  min-width: 0;
}

.settings-right {
  display: flex;
  align-items: stretch;
  flex: 1;
  min-width: 0;
}

.settings-card {
  background: rgba(255, 255, 255, 0.98);
  border-radius: 6px;
  padding: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: none;
  width: 100%;
  box-sizing: border-box;
}

.settings-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 30px 60px rgba(0, 0, 0, 0.2);
}

.card-header {
  font-weight: 600;
  color: #2c3e50;
  font-size: 14px;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid #f1f3f4;
}

.user-info-section {
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.user-avatar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.avatar {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 2px 6px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.avatar:hover {
  transform: scale(1.02);
  box-shadow: 0 6px 18px rgba(102, 126, 234, 0.4);
}

.user-details h3 {
  margin: 0 0 2px 0;
  color: #2c3e50;
  font-weight: 600;
  font-size: 16px;
}

.user-details p {
  margin: 0 0 4px 0;
  color: #7f8c8d;
  font-size: 12px;
}

.divider {
  height: 1px;
  background: #e1e8ed;
  margin: 8px 0;
}

.profile-form {
  max-width: 100%;
  width: 100%;
}

.modern-input {
  border-radius: 12px;
  padding: 16px 20px;
  background: #f8f9fa;
  border: 2px solid transparent;
  transition: all 0.3s ease;
}

.modern-input:hover .el-input__wrapper {
  box-shadow: 0 0 0 1px rgba(102, 126, 234, 0.2) inset;
}

.modern-input .el-input__wrapper {
  border-radius: 8px;
  background: #f8f9fa;
  border: 1px solid transparent;
  transition: all 0.3s ease;
  padding: 8px 12px;
}

.modern-input .el-input__wrapper.is-focus {
  border-color: #667eea;
  background: white;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.disabled-input .el-input__wrapper {
  background: #f1f3f4;
  border-color: #e1e8ed;
}

.input-tip {
  font-size: 12px;
  color: #95a5a6;
  font-weight: 500;
}

.update-btn {
  width: 100%;
  height: 32px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 1px 3px rgba(102, 126, 234, 0.3);
}

.update-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.account-info {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.info-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px;
  background: #f8f9fa;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.info-item:hover {
  background: #e9ecef;
  transform: translateX(5px);
}

.info-icon {
  width: 28px;
  height: 28px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 12px;
  box-shadow: 0 1px 3px rgba(102, 126, 234, 0.3);
}

.info-content {
  flex: 1;
}

.info-label {
  font-weight: 600;
  color: #2c3e50;
  font-size: 12px;
  margin-bottom: 2px;
}

.info-value {
  color: #7f8c8d;
  font-size: 13px;
  font-weight: 500;
}

.password-card {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.password-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.form-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

:deep(.el-form-item) {
  margin-bottom: 8px;
}

:deep(.el-form-item__label) {
  height: 28px;
  line-height: 28px;
  font-size: 13px;
}

:deep(.el-input__wrapper) {
  padding: 6px 10px;
  min-height: 32px;
}

.password-btn {
  flex: 1;
  height: 32px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 4px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 1px 3px rgba(102, 126, 234, 0.3);
}

.password-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.reset-btn {
  flex: 1;
  height: 32px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 4px;
  background: #f8f9fa;
  border: 1px solid #e9ecef;
  color: #6c757d;
  transition: all 0.3s ease;
}

.reset-btn:hover {
  background: #e9ecef;
  border-color: #dee2e6;
  color: #495057;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .settings-container {
    max-width: 98%;
    padding: 0 12px;
  }
}

@media (max-width: 900px) {
  .settings-content {
    flex-direction: column;
    gap: 8px;
  }
  
  .settings-left,
  .settings-right {
    flex: 1;
    width: 100%;
  }
  
  .settings-card {
    padding: 8px;
  }
}

@media (max-width: 768px) {
  .settings-page {
    padding: 5px;
  }
  
  .settings-container {
    padding: 0 4px;
  }
  
  .settings-header h2 {
    font-size: 18px;
  }
  
  .settings-header p {
    font-size: 12px;
  }
  
  .settings-card {
    padding: 6px;
    margin: 0;
  }
  
  .user-avatar {
    flex-direction: column;
    text-align: center;
    gap: 8px;
  }
  
  .form-actions {
    flex-direction: column;
    gap: 4px;
  }
  
  .card-header {
    font-size: 13px;
    margin-bottom: 6px;
  }
  
  .el-form-item {
    margin-bottom: 6px;
  }
}

@media (max-width: 480px) {
  .settings-page {
    padding: 5px;
  }
  
  .settings-container {
    padding: 0 5px;
  }
  
  .settings-header h2 {
    font-size: 20px;
  }
  
  .settings-card {
    padding: 16px 12px;
    border-radius: 8px;
  }
  
  .el-form-item__label {
    padding-right: 8px;
    font-size: 14px;
  }
  
  .modern-input .el-input__wrapper {
    padding: 12px;
  }
}
</style>